<template>
	<view class="content">
		<scroll-view scroll-y="true" class="scroll-height">
			<view class="condition">
				<view class="conmenus">
					<view class="conleft">{{$t('defect.defInfo.finishTime')}}<span class="redx">*</span></view>
					<view class="conright">
						<date-picker mode="dateTime" :showValue="form.clearDate" @change="dateChange">
							{{ form.clearDate || $t('baseCommon.message.pleaseSelect', { title: '' }) }}
						</date-picker>
					</view>
				</view>
				<view class="conmenus">
					<view class="conleft">{{$t('defect.defInfo.phenomenonIsCorrect')}}<span class="redx">*</span></view>
					<view class="conright">
						<radio-group @change="radioChange">
							<label class="radio-label">
								<view><radio class="radio-item" value="1" :checked="'1' == form.isCorrect" /></view>
								<view>{{$t('baseCommon.status.yes')}}</view>
							</label>
							<label class="radio-label">
								<view><radio class="radio-item" value="0" :checked="'0' == form.isCorrect" /></view>
								<view>{{$t('baseCommon.status.no')}}</view>
							</label>
						</radio-group>
					</view>
				</view>
				<view class="contexts">
					<view class="conleft">{{$t('defect.defInfo.maintenanceExplain')}}<span class="redx">*</span></view>
					<view class="contopright">
						<textarea :placeholder="$t('baseCommon.message.pleaseEnter', { title: '' })" maxlength="-1" auto-height v-model="form.maintenanceExplain"/>
					</view>
				</view>
			</view>
			<view class="label" v-if="form.isCorrect == '0'">
				<view class="label-content">
					<view class="label-title">
						<view class="label-mark"></view>
						<text>{{$t('defect.common.defectCom')}}</text>
					</view>
				</view>
				<view class="condition">
					<view class="contexts">
						<view class="conleft">{{$t('defect.defInfo.oldEqTypeName')}}</view>
						<view class="contopright readonly">
							{{formData.oldEqTypeName}}
						</view>
					</view>
					<view class="contexts">
						<view class="conleft">{{$t('defect.defInfo.eqTypeName')}}</view>
						<view class="coninputright">
							<input @click="selectEqType" disabled :placeholder="$t('baseCommon.message.pleaseSelect', { title: '' })" :value="form.actualEqTypeName"></input>
						</view>
					</view>
				</view>
				<view class="condition">
					<view class="conmenus">
						<view class="conleft">{{$t('defect.defInfo.oldFaultCode')}}</view>
						<view class="conright readonly">
							{{formData.oldFaultCode}}
						</view>
					</view>
					<view class="conmenus">
						<view class="conleft">{{$t('defect.defInfo.faultCode')}}</view>
						<view class="conright">
							<input :value="form.actualFaultCode"></input>
						</view>
					</view>
				</view>
				<view class="condition">
					<view class="contexts">
						<view class="conleft">{{$t('defect.defInfo.oldFaultDescription')}}</view>
						<view class="contopright readonly">
							{{formData.oldFaultDescription}}
						</view>
					</view>
					<view class="contexts">
						<view class="conleft">{{$t('defect.defInfo.faultDescription')}}</view>
						<view class="coninputright">
							<input @click="selectPhenomenon" disabled :placeholder="$t('baseCommon.message.pleaseSelect', { title: '' })" :value="form.actualFaultDescription"></input>
						</view>
					</view>
				</view>
				<view class="condition">
					<view class="contexts">
						<view class="conleft">{{$t('defect.defInfo.oldReasonAnalysis')}}</view>
						<view class="contopright readonly">
							{{formData.oldReasonAnalysis}}
						</view>
					</view>
					<view class="contexts">
						<view class="conleft">{{$t('defect.defInfo.faultReasonDesc')}}</view>
						<view class="contopright">
							<textarea :placeholder="$t('baseCommon.message.pleaseEnter', { title: '' })" auto-height v-model="form.actualReasonAnalysis"/>
						</view>
					</view>
				</view>
				<view class="condition">
					<view class="contexts">
						<view class="conleft">{{$t('defect.defInfo.oldTreatmentMeasure')}}</view>
						<view class="contopright readonly">
							{{formData.oldTreatmentMeasure}}
						</view>
					</view>
					<view class="contexts">
						<view class="conleft">{{$t('defect.defInfo.faultSchemeDesc')}}</view>
						<view class="contopright">
							<textarea :placeholder="$t('baseCommon.message.pleaseEnter', { title: '' })" auto-height v-model="form.actualTreatmentMeasure"/>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<view class="button-groups">
			<button type="primary" @click="submit">{{$t('baseCommon.buttons.submit')}}</button>
			<button type="primary" @click="cancel">{{$t('baseCommon.buttons.cancel')}}</button>
		</view>
	</view>
</template>

<script>
	import { notice, utilities } from '@/utils/common.js'
	import { deleteFaultReport, addFaultReport }  from '@/api/business/workorder.js'
	
	export default {
		data() {
			return {
				formData: {},
				form: {
					clearDate: '',
					applicantId: '',
					applicant: '',
					applyTime: '',
					assetTypeId: '',
					isCorrect: '',
					maintenanceExplain: '',
					actualPhenomenonId: '',
					actualEqTypeId: "",
					actualEqTypeName: "",
					actualFaultCode: "",
					actualFaultDescription: "",
					actualReasonAnalysis: "",
					actualTreatmentMeasure: "",
				}
			}
		},
		onLoad(option) {
			this.formData = JSON.parse(option.form)
			this.init()
		},
		methods: {
			init() {
				this.form.clearDate = utilities.getNowDateTime()
				this.form.applicantId = uni.getStorageSync("userId")
				this.form.applicant = uni.getStorageSync("userName")
				this.form.applyTime = utilities.getNowDateTime()
				this.form.assetTypeId = this.formData.assetTypeId ? this.formData.assetTypeId : ""
				this.form.actualEqTypeId = this.formData.oldEqTypeId
				this.form.actualEqTypeName = this.formData.oldEqTypeName
				this.form.actualFaultCode = this.formData.oldFaultCode
				this.form.actualFaultDescription = this.formData.oldFaultDescription
				this.form.actualReasonAnalysis = this.formData.oldReasonAnalysis
				this.form.actualTreatmentMeasure = this.formData.oldTreatmentMeasure
			},
			dateChange(e) {
				this.form.clearDate = e
			},
			radioChange(e) {
				this.form.isCorrect = e.target.value
			},
			selectEqType() {
				let that = this
				uni.navigateTo({
					url: "/pages/business/defect/common/select-eqType?eqTypeId="+that.form.actualEqTypeId,
					events: {
						selectEqType(data) {
							that.form.actualEqTypeId = data.eqTypeId
							that.form.actualEqTypeName = data.eqTypeName
						}
					}
				})
			},
			selectPhenomenon() {
				let that = this
				uni.navigateTo({
					url: '/pages/business/defect/common/select-phenomenon?eqTypeId='+ that.form.actualEqTypeId +'&assetTypeId='+ that.form.assetTypeId +'&phenomenon=' + that.form.actualPhenomenonId,
					events: {
						selectPhenomenon(data) {
							that.form.actualPhenomenonId = data.id
							that.form.actualFaultCode = data.faultCode
							that.form.actualFaultDescription = data.faultDescription
							that.form.actualReasonAnalysis = data.reasonAnalysis
							that.form.actualTreatmentMeasure = data.treatmentMeasure
						}
					}
				})
			},
			submit() {
				if(!this.form.clearDate){
					notice.toast(this.$t('baseCommon.message.pleaseSelect', { title: this.$t('defect.defInfo.finishTime') }))
					return
				}
				if(!this.form.isCorrect){
					notice.toast(this.$t('baseCommon.message.pleaseSelect', { title: this.$t('defect.defInfo.phenomenonIsCorrect') }))
					return
				}
				if(!this.form.maintenanceExplain){
					notice.toast(this.$t('baseCommon.message.pleaseEnter', { title: this.$t('defect.defInfo.maintenanceExplain') }))
					return
				}
				
				let param = {
					"woId": this.formData.woId,
					"locaId": this.formData.locaId,
					"locaNo": this.formData.locaNo,
					"locaName": this.formData.locaName,
					"assetId": this.formData.assetId,
					"assetNo": this.formData.assetNo,
					"assetName": this.formData.assetName,
					"manageObjNo": this.formData.manageObjNo,
					"manageObjName": this.formData.manageObjName,
					"skillId": this.formData.skillId,
					"faultDate": this.formData.createTime,
					"remark": this.formData.remark,
					"faultTypeNo": this.form.actualFaultCode,
					"faultTypeName": "",
					"faultSymptonDesc": this.form.actualFaultDescription,
					"faultReasonDesc": this.form.actualReasonAnalysis,
					"faultSchemeDesc": this.form.actualTreatmentMeasure,
					"eqTypeId": this.form.actualEqTypeId,
					"eqTypeName": this.form.actualEqTypeName,
					"analysiser": uni.getStorageSync("userName"),
					"analysisTime": utilities.getNowDateTime(),
					"createUser": uni.getStorageSync("userName")
				}
				// 先删除再保存，保证只有一条数据
				deleteFaultReport({ woId: this.formData.woId }).then(()=>{
					addFaultReport(param)
				})
				
				this.formData.clearDate = this.form.clearDate
				this.formData.maintenanceExplain = this.form.maintenanceExplain
				this.formData.applyTime = this.form.applyTime
				this.formData.applyer = this.form.applicantId
				this.formData.applyerName = this.form.applicant
				this.formData.changeTime = this.form.clearDate
				this.formData.changeDesp = this.form.maintenanceExplain
				
				const eventChannel = this.getOpenerEventChannel();
				eventChannel.emit('handleCheck', this.formData);
				uni.navigateBack();
			},
			cancel() {
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/style/common-detail.scss';
	
	.scroll-height {
		/* #ifdef APP-PLUS */
		height: calc(100vh - 65px);
		/* #endif*/
		/* #ifdef H5 */
		height: calc(100vh - 105px);
		/* #endif*/
	}
	
	uni-radio-group {
	    display: inline-flex;
	}
	
	.radio-label {
		display: flex;
		margin: 0 5px;
	}
	
	.radio-item {
		transform: scale(0.7);
	}
	
	.readonly {
		color: #999;
	}
</style>
